<?php

class User extends Atom {
  
  
  public function auth( $id, $cookie ) {
    $sql = "
        SELECT * FROM `login`
        WHERE `uid`=':i1'
          AND `hash`='::2'
          AND ( `ip`='::3' OR NOT `ip_check` )";
    $res = db::q( $sql, $id, $cookie, $_SERVER['REMOTE_ADDR'] );
    if ( $r = $res->assoc() ) {
      $sql = "SELECT * FROM `users` WHERE `id`=':i1'";
      $this->data = db::q( $sql, $id )->assoc();
      return true;
    } else {
      return false;
    }
  }
  
  
  public function online() {
    return (bool)$this->id;
  }
  
  public function guest() {
  	return $this->id===0;
  }
  
  public function id() {
    return $this->id;
  }
  
  
  public function name() {
    return $this->name;
  }
  
  
  public function rights() {
    return $this->rights;
  }
  
}

system::$user = new User();

if ( isset($_COOKIE['id']) && isset($_COOKIE['auth']) ) {
  system::$user->auth( $_COOKIE['id'], $_COOKIE['auth'] );
}


?>